<?php

namespace  app\test\controller;
use app\test\model\User as UserModel;
use app\test\model\Profile;
use think\Validate;

use app\index\model\Book;

use think\Controller;



class User extends Controller
{
    public  function  add()
    {
/*   //Model      对象添加成员
        $user=new UserModel();
        $user->nickname='小李';
        $user->email="11@11DD.com";
        $user->birthday=strtotime('1999-09-09');
        
        if($user->save())
        {
            return '用户['.$user->nickname.':'.$user->id.']新增成功';
        }else{
            return $user->getError();
        }
          */
        
/*          //第二种方式： 数组方式添加成员
        $user['nickname'] = '看云';
        $user['email']    = 'kancloud@qq.com';
        $user['birthday'] = strtotime('2015-04-02');
        if ($result = UserModel::create($user)) {
            return '用户[ ' . $result->nickname . ':' . $result->id . ' ]新增成功';
        } else {
            return '新增出错';
        } */
      
/*         $user           = new UserModel;
        $user->nickname = '小张';
        $user->email    = 'thinkphp@qq.com';
        $user->birthday = '1977-03-05';
        if ($user->save()) {
            return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
        } else {
            return $user->getError();
        } */
        
        
        
/*
 * //   1             
        $user = new UserModel;   
        
       // if ($user->save(input('post.'))) 
        if ($user->validate(true)->save(input('post.')))
        {
            return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
        } else {
            return $user->getError();
        } 
        
         */
        
 /*        
       $user = new UserModel;
       $data=input('post.');

       if ($user->validate(true)->save($data))
       {
           return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
       } else {
           return $user->getError();
       }
       
        */
       
/*        
       $data=input('post.');
       $result=$this->validate($data,'User'); //User 匹配的是validate目录下User
       
        if (true !== $result) {
            return $result;
        }
        $user = new UserModel;
        // 数据保存
        $user->save($data);
        return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
        
        */
        
        
       $data  = input('post.');
       // 验证birthday是否有效的日期
       $check = Validate::is($data['birthday'],'date');
       if (false === $check) {
           return 'birthday日期格式非法';
       }
       $user  = new UserModel;
       // 数据保存
       $user->save($data);
       return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
        
        
        
        
        
        
//        $data=input('post.');
//         print_r($data);
        
        
  
       
/*         $check=Validate::is($data['birthday'],'date');
       
       if(false==$check)
           return 'birthday is wrong'; 
           
       $user=new UserModel();
       
       
       $user->save($data);
       
      
       
       return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
   */    
        
    }
    
    
    // 批量新增用户数据
    public function addList()
    {
        $user = new UserModel;
        $list = [
            ['nickname' => '张三', 'email' => 'zhanghsan@qq.com', 'birthday' => strtotime('1988-01-15')],
            ['nickname' => '李四', 'email' => 'lisi@qq.com', 'birthday' => strtotime('1990-09-19')],
        ];
        if ($user->saveAll($list)) {
            return '用户批量新增成功';
        } else {
            return $user->getError();
        }
    }
    //UserModel::get 与Db find区别
    //模型的get方法返回的一定是 当前模型对象实例
    //Db类默认返回的数组
    public function read($id='1')
    {
        
        
/*         $user=UserModel::get($id);
        echo $user->nickname . '<br/>';
        echo $user->email . '<br/>';  
        echo date('Y/M/D',$user->birthday).'<br />'; */
        
        // get方法返回的对象
        //但是系统为模型实现了ArrayAccess接口，因此可以通过数组的方式访问
      
       // $user = UserModel::get($id);
       // $user = UserModel::getByEmail('11@11.com');
        
      //  $user = UserModel::get(['nickname'=>'小李']);
      
       // $user = UserModel::where('nickname', '看云')->find();
        $user=UserModel::get($id);
        
    echo $user->nickname . '<br/>';
    echo $user->email . '<br/>';
    echo $user->birthday . '<br/>';
    echo $user->status . '<br/>';
    echo $user->create_time . '<br/>';
    echo $user->update_time . '<br/>';
     
      //  echo $user['user_birthday'].'<br>';
        //echo date('Y/m/d', $user['birthday']) . '<br/>';
        
        
        
       
    }
    
    public function index(){
       
        //$list=UserModel::all();
       // $list=UserModel::all(['status'=>0]);
       // $list=UserModel::where('id','<',10)->select();
      //  $list=UserModel::scope('email,status')->all();
       // $list=UserModel::scope('email')->all();
        $list=UserModel::scope('email','11@11.com')
     //   ->scope('status')
//         ->scope(function ($query){
//             $query->order('id','asc');
//         })
        ->all();
        foreach($list as $user){
            echo $user->id . '<br/>';
            echo $user->nickname . '<br/>';
            echo $user->email . '<br/>';
            echo $user->birthday. '<br/>';
            echo $user->status. '<br/>';
            echo '----------------------------------<br/>';
        }
    }
    
    // 更新用户数据
    public function update($id)
    {
/*         $user           = UserModel::get($id);
        $user->nickname = '刘晨e';
        $user->email    = 'liu21st@gmail.com';
        if (false !== $user->save()) {
            return '更新用户成功';
        } else {
            return $user->getError();
        } */
        
        
        
        $user['id']       = (int) $id;
        $user['nickname'] = '刘晨';
        $user['email']    = 'liu21st@gmail.com';
        $result           = UserModel::update($user);
        if (false !== $result) {
            return '更新用户成功';
        } else {
            return $user->getError();
        }
    }
    
   
    
    // 删除用户数据
    public function delete($id)
    {
/*         $user = UserModel::get($id);
        if ($user) {
            $user->delete();
            return '删除用户成功';
        } else {
            return '删除的用户不存在';
        } */
        
        $result = UserModel::destroy($id);
        if ($result) {
            return '删除用户成功';
        } else {
            return '删除的用户不存在';
        }
        
    }
    
    
    public function create()
    {
        
      //  return phpinfo();
        
       return view();
    }
    
    
    public  function allRelation(){
        
        $user           = new UserModel;
        $user->name     = 'thinkphp';
      //  $user->password = '123456';
        $user->nickname = '流年T';
        if ($user->save()) {
            
            
            // 写入关联数据
/*             $profile           = new Profile;
            $profile->truename = '刘晨T';
            $profile->birthday = '1977-03-05';
            $profile->address  = '中国上海';
            $profile->email    = 'thinkphp@qq.com'; */
            
            $profile['truename'] = '刘晨';
            $profile['birthday'] = '1977-03-05';
            $profile['address']  = '中国上海';
            $profile['email']    = 'thinkphp@qq.com';
            
            
            $user->profile()->save($profile);
            return '用户新增成功';
        } else {
            return $user->getError();
        }
           
    }
    
    public function updateR($id)
    {
/*         $user       = UserModel::get($id);
        $user->name = 'framework';
        if ($user->save()) {
            // 更新关联数据
            $user->profile->email = 'liu21st@gmail.com';
            $user->profile->save();
            return '用户[ ' . $user->name . ' ]更新成功';
        } else {
            return $user->getError();
        }
         */
        
/*         $user        = UserModel::get($id);
        $book        = $user->books()->getByTitle('ThinkPHP5开发手册');
        $book->title = 'ThinkPHP5快速入门';
        $book->save(); */
        
        
        $user = UserModel::get($id);
        $user->books()->where('title', 'ThinkPHP5快速入门')->update(['title' => 'ThinkPHP5开发手册']);
    }
    
    
    public function deleteR($id)
    {
        $user = UserModel::get($id);
        if ($user->delete()) {
            // 删除关联数据
            $user->profile->delete();
            return '用户[ ' . $user->name . ' ]删除成功';
        } else {
            return $user->getError();
        }
        
        
        
    }
    
    
    public function  readR($id){
/*         $user=UserModel::get($id,'profile');
        echo $user->name . '<br/>';
        echo $user->nickname . '<br/>';
        echo $user->profile->truename . '<br/>';
        echo $user->profile->email . '<br/>'; */
        //一对多预载入查询会在原先延迟查询的基础上增加一次查询，可以解决典型的N+1次查询问题
        
//         $user  = UserModel::get($id,'books');
//        // $books = $user->books;
//       //  $books = $user->books()->where('status',0)->select();
//         $book  = $user->books()->getByTitle('ThinkPHP5快速入门');
        
     
        
        
        // 查询有写过书的作者列表
      //  $user = UserModel::has('books')->select();
        // 查询写过三本书以上的作者
    //    $user = UserModel::has('books', '>=', 3)->select();
        // 查询写过ThinkPHP5快速入门的作者
        $user = UserModel::hasWhere('books', ['title' => 'ThinkPHP5快速入门'])->select();
        dump($user);
    }

    
    public function addBook($id){
    
        $user               = UserModel::get($id);
        $book               = new Book;
    
        /*
         $book->title        = 'ThinkPHP5快速入门';
         $book->publish_time = '2016-05-06';
         $user->books()->save($book); */
    
    
    
    
        $books = [
            ['title' => 'ThinkPHP5快速入门', 'publish_time' => '2016-05-06'],
            ['title' => 'ThinkPHP5开发手册', 'publish_time' => '2016-03-06'],
        ];
        $user->books()->saveAll($books);
    
    
        return '添加Book成功';
    }
    
}






/* 


有一种方式可以让你省去别名定义，系统支持统一对控制器类添加Controller后缀，修改配置参数：

// 是否启用控制器类后缀
'controller_suffix'  => true,
然后，控制器类文件改为UserController.php，并且修改控制器类的定义如下：

<?php
namespace app\index\controller;

use app\index\model\User;

class UserController
{
    // 新增用户数据
    public function add()
    {
        $user           = new User;
        $user->nickname = '流年';
        $user->email    = 'thinkphp@qq.com';
        $user->birthday = strtotime('1977-03-05');
        if ($user->save()) {
            return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
        } else {
            return $user->getError();
        }
    }
}
接下来，我们访问

http://tp5.com/user/add
 */